<?php
//消费券回调地址设置

namespace App\Console\Commands;

use App\Services\Wx\WxConfig;
use App\Services\Wx\WxCoupon;
use Illuminate\Console\Command;

class WxCouponCallbackUrl extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wx:coupon:callback:url {actionType : 操作类型}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '消费券回调地址设置';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $type = $this->argument('actionType');
        if (!method_exists($this, $type)) {
            exit('error');
        }
        $this->$type();

        return true;
    }

    /**
     * 设置消费券回调地址
     * @return void
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function setUrl() {
        $app = (new WxCoupon())->getApp();
        $api = $app->getClient();
        $res = $api->postJson('/v3/marketing/favor/callbacks', [
            'mchid' => (string)env('MCHID'),
            'notify_url' => env('APP_URL') . '/wx/coupon/userconsumecallback',
        ]);
        dd($res->toArray());
    }

    /**
     * 获取消费券回调地址
     * @return void
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getUrl() {
        $app = (new WxCoupon())->getApp();
        $api = $app->getClient();
        $res = $api->get('/v3/marketing/favor/callbacks', [
            'mchid' => env('MCHID'),
        ]);
        dd($res->toArray());
    }

}
